package Interface;

import MyEngine.ABEngine;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import remotechessplayer.RemoteChessPlayer;


/*
 * InterfaceJPanel.java
 *
 * Created on 2011-03-28, 21:12:11
 */

/**
 *
 * @author agnieszka
 */
public class InterfaceJPanel extends javax.swing.JPanel {
     
    public boolean  bBsave,bBrun, bBstop ,bChange, bPlay;       //the flags of buttons: RUN, STOP, the flag bChange settings
                                                                //  bBsave move
    public boolean bRb1_ous,bRb1_ne,bRb1_eo, bRb1_us,bRb2_ous,bRb2_ne,bRb2_eo, bRb2_us,
             bRb3_Player1, bRb3_Player2;   //the flags of radio buttons from goup1 and group2 (Player1 and Player2)
    public boolean bBsettings1, bBsettings2,yield=true,bBload, bBnew, bBHelp=false;;

    public int who_is_player1=0, who_is_player2=0, what_figure=1000;

    public Thread run;
    
    public HelpPanel instruction= new HelpPanel();;
    
    public Play players;

    public OutEngine outboard1 = new OutEngine();
    public OutEngine outboard2 = new OutEngine();

    public ABEngine abe1 = new ABEngine();

    public RemoteChessPlayer onlineplayer1 = new RemoteChessPlayer(),onlineplayer2 = new RemoteChessPlayer();;

    public Action actionofgame;
    


    /** Creates new form InterfaceJPanel */
    public InterfaceJPanel() {
        
            initComponents();
            instruction.instruction_window();
            actionofgame = new Action(this);

            bChange=true;
            change();

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jPanel_Board = new javax.swing.JPanel();
        thinkingBar = new javax.swing.JProgressBar();
        thinkingLabel = new javax.swing.JLabel();

        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setPreferredSize(new java.awt.Dimension(1260, 670));

        javax.swing.GroupLayout jPanel_BoardLayout = new javax.swing.GroupLayout(jPanel_Board);
        jPanel_Board.setLayout(jPanel_BoardLayout);
        jPanel_BoardLayout.setHorizontalGroup(
            jPanel_BoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 787, Short.MAX_VALUE)
        );
        jPanel_BoardLayout.setVerticalGroup(
            jPanel_BoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 582, Short.MAX_VALUE)
        );

        thinkingBar.setIndeterminate(true);

        thinkingLabel.setText("Thinking...");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(199, 199, 199)
                        .addComponent(thinkingLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(thinkingBar, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel_Board, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jPanel_Board, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(thinkingBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(thinkingLabel))
                .addContainerGap(53, Short.MAX_VALUE))
        );

        thinkingBar.setVisible(false);
        thinkingLabel.setVisible(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JPanel jPanel1;
    public javax.swing.JPanel jPanel_Board;
    public javax.swing.JProgressBar thinkingBar;
    public javax.swing.JLabel thinkingLabel;
    // End of variables declaration//GEN-END:variables




    private void execute() {      //which the engine, and when can be executed

            players = new Play(who_is_player1,who_is_player2,this);

            if(bRb1_eo && bBrun && bRb2_us){

                bBrun=false;

                    try {
                        outboard1.mozg();
                    } catch (Exception ex) {
                       System.out.println("Problem load - outboard / human");
                    }

                run = new Thread( players,"Outboard-Human");
                run.start();
            }


            if(bRb2_eo && bBrun && bRb1_us){

                bBrun=false;

                    try {
                        outboard2.mozg();

                    } catch (Exception ex) {
                        System.out.println("Problem load - human / outboard");
                    }

                run = new Thread( players,"Human-Outboard");
                run.start();
            }

            if(bRb1_ne && bRb2_us &&bBrun){

                bBrun=false;
                run = new Thread( players,"Neural-Human");
                run.start();
            }

             if(bRb2_ne && bRb1_us && bBrun){

                bBrun=false;
                run = new Thread( players,"Human-Neural");
                run.start();
             }


                else if (bBrun)  {

                    run = new Thread(players,"Humans");
                    run.start();
                    System.out.println("wl run");

                    bBrun=false;

                }


           if((bRb2_eo || bRb1_eo) && bBstop) {

               outboard1.eout.destroy();
               outboard2.eout.destroy();
            }

           change();
    }

    public JPanel getPanel() {
    
        return jPanel_Board;
        
    }

    boolean getbPlay() {
        
        return this.bPlay;
    }

    public void change() {

        this.updateUI();
    }


    public void actionNewGame(java.awt.event.ActionEvent evt){
       bBnew=true;
       actionofgame.zerogame=true;
       actionofgame.load=false;
       
       MainInterface.exchange.resetBoardState();
       actionofgame.makeboard();
      
       bChange=false;
       change();
    }
    public void actionRun(java.awt.event.ActionEvent evt){
        
        bBrun=true;
        bPlay=true;
        bChange=false;
        yield=false;
        
        actionofgame.settings1_is_on=false;
        actionofgame.settings2_is_on=false;
        actionofgame.window_settings1.dispose();
        actionofgame.window_settings2.dispose();
        execute();
    }

    void p2RBOnlineSetting(ActionEvent e) {
       bBsettings2=true;
       actionofgame.configure2();
    }

    void p1RBOnlineSetting(ActionEvent e) {
        bBsettings1=true;
        actionofgame.configure1();
    }

    public void actionStop(ActionEvent e){
       
        bPlay=false;
        actionofgame.load=false;
        run.stop();

        players.chessboard.highlightSquares.get(0).show(false);
        players.chessboard.highlightSquares.get(1).show(false);

        MainInterface.exchange.stopGame();

        System.out.println("zabilam watek?");
        yield=true;
        bBstop=true;
        bChange=true;
        bBload=true;
        actionofgame.zerogame=false;
        change();
    }

    void actionSaveGame(ActionEvent e) {
        bBsave=true;
        actionofgame.savegame();

    }

    void actionHelp(ActionEvent e) {
        bBHelp=true;
        instruction.okodane.setVisible(true);
    }

    void actionLoadGame(ActionEvent e) {
         bBload=true;
         actionofgame.zerogame=false;
         actionofgame.loadgame();

         bChange=false;
         change();
    }

    void actionTrain(ActionEvent e) {

        // TODO add your handling code here:
    }
    public void rb2OnlineActionPerformed1(java.awt.event.ActionEvent evt) {
             bRb2_ous=true;
             bRb2_ne=false;
             bRb2_eo=false;
             bRb2_us=false;
             
             who_is_player2=1;
    }
    
    public void rb1OutboardEActionPerformed1(java.awt.event.ActionEvent evt) {
            bRb1_eo=true;
            bRb1_ous=false;
            bRb1_ne=false;
            bRb1_us=false;
//          
            who_is_player1=3;
}

    public void rb2NeuralActionPerformed1(java.awt.event.ActionEvent evt) {
            bRb2_ne=true;
            bRb2_ous=false;
            bRb2_eo=false;
            bRb2_us=false;
           who_is_player2=2;
    }

    public void rb2UserActionPerformed1(java.awt.event.ActionEvent evt) {
            bRb2_us=true;
            bRb2_ous=false;
            bRb2_ne=false;
            bRb2_eo=false;
            who_is_player2=0;
    }

    public void rb2OutboardEActionPerformed1(java.awt.event.ActionEvent evt) {
            bRb2_eo=true;
            bRb2_ous=false;
            bRb2_ne=false;
            bRb2_us=false;
            who_is_player2=3;
    }

    public void rb1NeuralActionPerformed1(java.awt.event.ActionEvent evt) {
            bRb1_ne=true;
            bRb1_ous=false;
            bRb1_eo=false;
            bRb1_us=false;
            who_is_player1=2;
    }

    public void rb1OnlineActionPerformed1(java.awt.event.ActionEvent evt) {
            bRb1_ous=true;
            bRb1_ne=false;
            bRb1_eo=false;
            bRb1_us=false;
            who_is_player1=1;
    }

    public void rb1UserActionPerformed1(java.awt.event.ActionEvent evt) {
            bRb1_us=true;
            bRb1_ous=false;
            bRb1_ne=false;
            bRb1_eo=false;
            who_is_player1=0;
    }
}